package com.mice.game.trigger{
	import com.iyimo.core.YEngine;
	import com.mice.game.core.Fighter;
	import com.mice.game.core.Player;
	import com.mice.meta.FighterMeta;
	import com.mice.meta.MetaHelper;
	

	public class AiTrigger{
		private static var _instance:AiTrigger;
		
		public var ai:Player;
		
		/**
		 * 技能冷却时间,现有一个stupid的方法实现吧
		 */		
		private var _tick:int = 150;
		
		public function AiTrigger(){}
		
		public static function get instance():AiTrigger{
			if (_instance == null)
				_instance = new AiTrigger();
			return _instance;
		}
		
		public function reset():void{
			_tick = 150;
		}
		
		public function update():void{
			_tick--;
			if (_tick > 0)return;
			_tick = 150;
			
			//level会从meta中读取.会叫个AiConfig
			var level:int = 1;
			var metaFeed:Array = MetaHelper.getFighterMetaInLevel(level, ai.team);
			var id:int = chooseOneFighter(metaFeed, ai.energy);
			if (id == -1)return;
			
			var f:Fighter = SpawnTrigger.instance.spawn(id, level, ai.team);
			YEngine.instance.layerManager.gameLayer.addChild(f.style);
		}
		
		/**
		 * 选择一个怪物
		 * -1说明没找到
		 */		
		private function chooseOneFighter(feed:Array, energy:int):int{
			for each (var meta:FighterMeta in feed){
				if (meta.costEnergy <= energy)
					return meta.id;
			}
			
			return -1;
		}
	}
}